package caffeine;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class Demo {
    public static void main(String[] args) throws InterruptedException {
        Cache<Object, Object> cache = Caffeine.newBuilder()
                .maximumSize(10)
                .expireAfterAccess(Duration.ofSeconds(5))
                .build();
        for (int i = 0; i < 1000; i++) {
            cache.put("u-" + i, new User(i, "user-" + i));
        }
        System.out.println("cache size = " + cache.getIfPresent("u-999"));
        TimeUnit.SECONDS.sleep(5);
        System.out.println("cache size = " + cache.getIfPresent("u-999"));
    }
}

@Data
@AllArgsConstructor
class User {
    private int id;
    private String name;
}
